package com.huodongrili.calender.data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.huodongrili.calender.data.DBTables.Lunar;

public class LunarData {

	private Context mContext;

	public LunarData(Context context) {
		mContext = context.getApplicationContext();
	}

	public void addLunar(Map<String, String> maps) {

		if (maps == null || maps.isEmpty()) {
			return;
		}

		SQLiteDatabase db = mContext.openOrCreateDatabase(
				Constant.LOCAL_DB_NAME, Context.MODE_WORLD_WRITEABLE, null);

		ContentValues cv = new ContentValues();

		Set<String> keys = maps.keySet();
		for (String key : keys) {

			cv.put(Lunar.DATE, key);
			cv.put(Lunar.DESC, maps.get(key));

			db.insert(Lunar.TABLE_NAME, null, cv);

		}
		
		db.close();

	}

	public void clearLunar() {
		SQLiteDatabase db = mContext.openOrCreateDatabase(
				Constant.LOCAL_DB_NAME, Context.MODE_WORLD_READABLE, null);

		String sql = "delete from " + Lunar.TABLE_NAME;
		db.execSQL(sql);
		
		db.close();

	}
	
	
	public Map<String, String> getLunarLimit(int year,int month){
		
		SQLiteDatabase db = mContext.openOrCreateDatabase(
				Constant.LOCAL_DB_NAME, Context.MODE_WORLD_READABLE, null);

		Cursor cursor = null;
		Map<String, String> maps = null;

		try {

			SimpleDateFormat sd = new SimpleDateFormat(Constant.DATE_FORMAT);
			
			Date dateS = new Date(year-1900,month,1);
			Date dateE = new Date(year-1900,month,31);
			String startStr = sd.format(dateS);
			String endStr = sd.format(dateE);
			
			cursor = db.rawQuery("select * from " + Lunar.TABLE_NAME+" where "+ Lunar.DATE +" > '"+startStr+"' and "+ Lunar.DATE+" < '"+ endStr+"'", null);
			int count = cursor.getCount();
			if (cursor != null && count > 0) {
				maps = new HashMap<String, String>(count);
				cursor.moveToFirst();
				while (!cursor.isAfterLast()) {
					String date = cursor.getString(cursor
							.getColumnIndex(Lunar.DATE));
					String desc = cursor.getString(cursor
							.getColumnIndex(Lunar.DESC));

					maps.put(date, desc);

					cursor.moveToNext();
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if (cursor != null) {
				cursor.close();
			}
			
			db.close();
		}

		return maps;
		
	}
	

	public Map<String, String> getLunars() {

		SQLiteDatabase db = mContext.openOrCreateDatabase(
				Constant.LOCAL_DB_NAME, Context.MODE_WORLD_READABLE, null);

		Cursor cursor = null;
		Map<String, String> maps = null;

		try {

			cursor = db.rawQuery("select * from " + Lunar.TABLE_NAME, null);
			int count = cursor.getCount();
			if (cursor != null && count > 0) {
				maps = new HashMap<String, String>(count);
				cursor.moveToFirst();
				while (!cursor.isAfterLast()) {
					String date = cursor.getString(cursor
							.getColumnIndex(Lunar.DATE));
					String desc = cursor.getString(cursor
							.getColumnIndex(Lunar.DESC));

					maps.put(date, desc);

					cursor.moveToNext();
				}

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			if (cursor != null) {
				cursor.close();
			}
			
			db.close();
		}

		return maps;

	}

}
